package cn.yuan.xiaoyu.testmodule.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import cn.yuan.xiaoyu.R;

/**
 * Created by yukuoyuan on 2017/9/7.
 * 这是一个定义的无线轮播图的自定义view
 */
public class ScrollBannerView extends RelativeLayout {
    private Context mContext;
    /**
     * 默认的指示器选中的颜色
     */
    private int mCheckedColor = Color.parseColor("#FF0000");
    /**
     * 默认的未选中的指示器的颜色
     */
    private int mUnCheckedColor = Color.parseColor("#fdfdfd");
    /**
     * 底部背景的颜色
     */
    private int mBottomBackGroundColor = Color.parseColor("#ededed");

    public ScrollBannerView(Context context) {
        this(context, null);
    }

    public ScrollBannerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public ScrollBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initAttribute(attrs);
    }

    /**
     * 初始化各个属性
     *
     * @param attrs 属性
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void initAttribute(AttributeSet attrs) {
        /**
         * 获取属性值
         */
        TypedArray a = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.WeekView, 0, 0);
        try {
            /**
             * 指示器选中的颜色
             */
            mCheckedColor = a.getColor(R.styleable.ScrollBannerView_checkedColor, mCheckedColor);
            /**
             * 指示器未选中的颜色
             */
            mUnCheckedColor = a.getColor(R.styleable.ScrollBannerView_unCheckedColor, mUnCheckedColor);
            /**
             * 底部背景的颜色
             */
            mBottomBackGroundColor = a.getColor(R.styleable.ScrollBannerView_bottomBackgroundColor, mBottomBackGroundColor);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            /**
             * 释放资源
             */
            a.recycle();
        }
        initView();
    }

    /**
     * 初始化Viewpager,指示器view
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void initView() {
        initViewpager();
        initBottomIndexView();
    }

    /**
     * 初始化底部的指示器view
     */
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    private void initBottomIndexView() {
        LinearLayout linearLayout = new LinearLayout(mContext);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100);
//        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        linearLayout.setLayoutParams(layoutParams);
        linearLayout.setBackgroundColor(mBottomBackGroundColor);
        linearLayout.setVerticalGravity(LinearLayout.HORIZONTAL);
        linearLayout.addView(getCircleIndexView(Color.RED));
        linearLayout.addView(getCircleIndexView(Color.BLUE));
        addView(linearLayout);
    }

    private CircleIndexView getCircleIndexView(int red) {
        CircleIndexView circleIndexView = new CircleIndexView(mContext);
        LinearLayout.LayoutParams layoutPsarams = new LinearLayout.LayoutParams(30, 30);
        circleIndexView.setLayoutParams(layoutPsarams);
        circleIndexView.setBackgroundColor(red);
        return circleIndexView;
    }

    /**
     * 初始化Viewpager
     */
    private void initViewpager() {
        /**
         * 清楚界面的所有view
         */
        removeAllViews();
        ViewPager mViewPager = new ViewPager(mContext);
        /**
         * 设置宽高都是填充父布局
         */
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mViewPager.setLayoutParams(layoutParams);
        addView(mViewPager);
    }
}
